package com.ccit.zhxy.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ccit.zhxy.pojo.Student;
import com.ccit.zhxy.service.StudentService;
import com.ccit.zhxy.util.MD5;
import com.ccit.zhxy.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 鹏少
 * @Date 2023/12/26
 * @Date 14:08
 */
@Api(tags = "学生控制器")
@RestController
@RequestMapping("/sms/studentController")
public class StudentController {

    @Autowired
    private StudentService studentService;


    @ApiOperation("查询学生信息,分页带条件")
    @GetMapping("/getStudentByOpr/{pageNo}/{pageSize}")
    public Result getStudentByOpr(
            @ApiParam("页码数") @PathVariable Integer pageNo,
            @ApiParam("页大小") @PathVariable Integer pageSize,
            @ApiParam("学生姓名") String name,
            @ApiParam("班级名称") String clazzName
    ) {
        Page<Student> page = new Page<>(pageNo, pageSize);
        page = studentService.lambdaQuery().like(name != null, Student::getName, name)
                .eq(clazzName != null, Student::getClazzName, clazzName).orderByAsc(Student::getId)
                .page(page);

        return Result.ok(page);

    }

    @ApiOperation("添加和修改学生信息")
    @PostMapping("/addOrUpdateStudent")
    public Result addOrUpdateStudent(
            @ApiParam("学生实体类接收参数")@RequestBody Student student){

        //对学生的密码进行加密
        String password = student.getPassword();
        student.setPassword(MD5.encrypt(password));
        studentService.saveOrUpdate(student);
        return Result.ok("操作成功");

    }

    @ApiOperation("删除一个或者多个学生信息")
    @DeleteMapping("/delStudentById")
    public Result delStudentById(
            @ApiParam("多个学生id的JSON") @RequestBody List<Integer> ids
    ){
        studentService.removeByIds(ids);
        return Result.ok();
    }

}
